package com.tanhua.dubbo.api;

import com.tanhua.dubbo.api.mongo.QuestionApi;
import com.tanhua.model.mongo.Question;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class QuestionApiImpl implements QuestionApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查出该问卷列表的所有试题
     *
     * @param questionnaireId
     * @return
     */
    @Override
    public List<Question> findByQuestionnaireId(String questionnaireId) {
        Query query = new Query(Criteria.where("questionnaireId").is(questionnaireId));
        return mongoTemplate.find(query, Question.class);
    }

    /**
     * 根据 id  查询
     * @param questionId
     * @return
     */
    @Override
    public Question findById(String questionId) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(questionId));
        return mongoTemplate.findOne(query, Question.class);
    }
}
